package com.chat.controller;

import cn.hutool.core.util.ObjectUtil;
import com.chat.domain.pojo.User;
import com.chat.service.GroupService;
import com.chat.service.UserService;
import com.chat.util.SecurityHolderUtil;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 页面控制器
 *
 * @author wb
 * @since 2024-08-20
 */

@Controller
public class PageController {

    @Value("${netty.port}")
    private Integer nettyPort;

    @Autowired
    private UserService userService;

    @Autowired
    private GroupService groupService;


    // 登录页面
    @GetMapping({"/", "/login", "/index"})
    public String login(HttpSession session) {
        // 检查用户是否登录
        User user = SecurityHolderUtil.getUser(session);
        if (ObjectUtil.isEmpty(user)) {
            return "login";
        }

        return "redirect:/chat";
    }

    // 内容
    @RequestMapping("/content")
    public String content(Model model, HttpSession session) {

        // 当前登录用户的ws地址
        model.addAttribute("ws", "ws://" + getLocalIp() + ":" + nettyPort + "/ws");

        // 获取当前登录用户Id
        Integer userId = SecurityHolderUtil.getUserId(session);

        // 好友
        model.addAttribute("friends", userService.getFriendsByUserId(userId));

        // 群聊
        model.addAttribute("groups", groupService.getGroupsByUserId(userId));

        return "main";
    }

    // 主页
    @RequestMapping("/chat")
    public String index(HttpSession session) {
        // 检查用户是否登录
        User user = SecurityHolderUtil.getUser(session);
        if (ObjectUtil.isEmpty(user)) {
            return "login";
        }

        return "index";
    }

    // 通讯录
    @GetMapping("/contact")
    public String contact() {
        return "contact";
    }

    // 详情
    @GetMapping("/detail/{chatId}")
    public String detail(@PathVariable String chatId, Model model) {
        Integer id = Integer.parseInt(chatId.substring(chatId.lastIndexOf("-") + 1));
        if (chatId.contains("user")) {
            model.addAttribute("user", userService.getById(id));
        } else if (chatId.contains("group")) {
            model.addAttribute("group", groupService.getById(id));
        }

        return "detail";
    }

    // 个人信息页面
    @GetMapping("/profile")
    public String updateProfile() {
        return "profile";
    }


    // 注册页面
    @GetMapping("/register")
    public String register(){
        return "register";
    }

    // 退出登录
    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        // 销毁 session
        session.invalidate();
        return "login";
    }


    /**
     * 获取本机 ip
     */
    public static String getLocalIp() {
        try {
            InetAddress ip = InetAddress.getLocalHost();
            return ip.getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
            throw new RuntimeException("获取本机 ip 失败");
        }
    }
}
